package mf.mfrpc.servlet;

import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import mfinder.impl.ThreadContext;

/**
 * ServletThreadContext是一个线程变量，继承自{@link ThreadContext}。
 * ServletThreadContext包含http的相关的变量，因而它们都是线程安全的。
 */
public class ServletThreadContext extends ThreadContext {

    private HttpServletRequest request;

    private HttpServletResponse response;

    private RequestMap requestMap;

    private PageContext pageContext;

    private ServletContext servletContext;

    public ServletThreadContext(Map<String, Object> contextMap) {
        super(contextMap);
    }

    /**
     * 获取当前线程副本中的ServletThreadContext。
     *
     * @return 前线程副本中的ServletThreadContext。
     */
    public static ServletThreadContext get() {
        return ThreadContext.get();
    }

    /**
     * Returns the HTTP page context.
     *
     * @return the HTTP page context.
     */
    public static PageContext setPageContext(PageContext pageContext) {
        return get().pageContext = pageContext;
    }

    /**
     * Returns the HTTP page context.
     *
     * @return the HTTP page context.
     */
    public static PageContext getPageContext() {
        return get().pageContext;
    }

    /**
     * Sets the HTTP servlet request object.
     *
     * @param request the HTTP servlet request object.
     */
    public static void setRequest(HttpServletRequest request) {
        get().request = request;
        get().requestMap = new RequestMap(request);
    }

    /**
     * Gets the HTTP servlet request object.
     *
     * @return the HTTP servlet request object.
     */
    public static HttpServletRequest getRequest() {
        return get().request;
    }

    /**
     * Gets the HTTP servlet request parameters.
     *
     * @return the HTTP servlet request parameters.
     */
    public static Map<String, String[]> getRequestParameters() {
        return get().requestMap;
    }

    /**
     * Sets the HTTP servlet response object.
     *
     * @param response the HTTP servlet response object.
     */
    public static void setResponse(HttpServletResponse response) {
        get().response = response;
    }

    /**
     * Gets the HTTP servlet response object.
     *
     * @return the HTTP servlet response object.
     */
    public static HttpServletResponse getResponse() {
        return get().response;

    }

    /**
     * Gets the servlet context.
     *
     * @return the servlet context.
     */
    public static ServletContext getServletContext() {
        return get().servletContext;
    }

    /**
     * Sets the current servlet context object
     *
     * @param servletContext The servlet context to use
     */
    public static void setServletContext(ServletContext servletContext) {
        get().servletContext = servletContext;
    }
}
